package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.common.consants.SysConstants;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.convert.PaymentConvert;
import com.bjpowernode.boss.dao.entity.PayWay;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.AjaxResult;
import com.bjpowernode.boss.mvc.models.form.payway.ConfigProductWayForm;
import com.bjpowernode.boss.mvc.models.pages.PageInfo;
import com.bjpowernode.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PayWayController extends BaseController {

    @Resource
    private PayWayService payWayService;

    @Resource
    private PaymentConvert paymentConvert;
    @GetMapping("/payment/way/pageList")
    public AjaxResult pageList(Integer pageNo, String productCode){
        pageNo = defPageNo(pageNo);
        IPage<PayWay> pageResult = payWayService.pageList(pageNo, SysConstants.PAGE_SIZE, productCode);

        PageInfo page = new PageInfo(pageResult.getCurrent(),pageResult.getPages());

        Map<String,Object> data = new HashMap<>();
        data.put("page",page);
        data.put("list", paymentConvert.toListPayWayVo(pageResult.getRecords()));

        return AjaxResult.ok(data);
    }

    /*支付方式字典接口*/
    @GetMapping("/payment/way/dicList")
    public AjaxResult payWayDicList(){
        List<DicEntity> dicPayWayList = payWayService.getPayWayDicListFromCache();
        return AjaxResult.ok(dicPayWayList);
    }

    /*支付类型的字典接口*/
    @GetMapping("/payment/way/type/dicList")
    public AjaxResult payTypeDicList(@RequestParam("wayCode") String payWayCode){
        if(StrUtil.isNotBlank(payWayCode)) {
            List<DicEntity> dicPayTypeList = payWayService.getPayTypeDicListFromCache(payWayCode.trim().toUpperCase());
            return AjaxResult.ok(dicPayTypeList);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARAM_ERR);
    }

    /*配置产品的支付方式*/
    @PostMapping("/payment/way/config")
    public AjaxResult configProductPayWay(@RequestBody @Validated ConfigProductWayForm configPayWayForm){
        payWayService.configProductPayWay(configPayWayForm);
        return AjaxResult.ok();
    }
}
